/**
 * @format
 * @Author: TC
 * @Date: 2021-09-28 15:48:43
 * @Last Modified by: TC
 * @Last Modified time: 2025-05-05 01:39:39
 */

import { _decorator, BlockInputEvents, Component, easing, Node, tween, v3 } from "cc";
import { app } from "../../App";
import { IUIData } from "../../Common/BaseInterface";
import { EFrameEventConst } from "../../Const/FrameEventConst";
import { UIUnit } from "../UI/UIUnit";
import { BaseComponent } from "./BaseComponent";
const { ccclass, property } = _decorator;

/**
 * UI基类
 */
@ccclass("BaseUI")
export class BaseUI extends BaseComponent {
    @property({ type: Node, displayName: "触摸拦截节点" })
    protected nodeBlockInput: Node = null;

    /**
     * ui配置信息
     */
    private _uiInfo: UIUnit = null;
    public get uiInfo(): UIUnit {
        return this._uiInfo;
    }
    //每次打开窗口传入的数据
    protected data: IUIData = null;
    public setData(data: IUIData): void {
        this.data = data;
    }
    protected init(): void {
        super.init();
        this.node.name = this._uiInfo.uiCfg.uiName;
        if (this.nodeBlockInput) {
            this.nodeBlockInput.addComponent(BlockInputEvents);
        }
    }

    protected registerEvent(): void {
        super.registerEvent();
        this.register(EFrameEventConst.UPDATE_UI + this._uiInfo.uiCfg.uiName, this.onRefresh);
    }

    protected unregisterEvent(): void {
        super.unregisterEvent();
    }

    /**
     * 设置ui信息
     * @param info
     */
    public setUIInfo(info: UIUnit): void {
        this._uiInfo = info;
    }

    protected onShow(): void {
        this.node.setSiblingIndex(this.node.parent?.children.length - 1 || 0);
        this.openAniStart();
        this._playOpenAni();
    }

    /**
     * 播放打开动画
     */
    private _playOpenAni(): void {
        if (!this._uiInfo.uiCfg.noOpenAni) {
            const content = this.node.getChildByName("content");
            tween(content).stop();
            content.scale = v3(1.2, 1.2, 1);
            tween(content)
                .to(0.2, { scale: v3(1, 1, 1) }, { easing: easing.backOut })
                .call(() => {
                    this.openAniEnd();
                })
                .start();
        } else {
            this.openAniEnd();
        }
    }

    /**
     * 窗口打开动画开始
     */
    protected openAniStart(): void {}
    /**
     * 窗口打开动画结束
     */
    protected openAniEnd(): void {
        //关闭其他全屏界面
        if (this._uiInfo.uiCfg.fullScreen) {
            app.uiMgr.closeAllFullUIs(this._uiInfo.uiCfg.uiName);
        }
    }

    /**
     * 关闭
     */
    protected closeUI(): void {
        app.uiMgr.closeUI(this._uiInfo.uiCfg.uiName, this._uiInfo.uuid);
    }

    /**
     * 销毁
     */
    protected destroyUI(): void {
        app.uiMgr.destroyUI(this._uiInfo.uiCfg.uiName, this._uiInfo.uuid);
    }
    /**
     * 首次打开
     */
    public openFirst(): void {
        this.onShow();
    }
    /**
     * 再次打开
     */
    public openAgain(): void {
        this.onShow();
    }
    onDisable() {}
}
